package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.OrderDetail;
import com.itheima.bean.Orders;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("orders:{}", orders);

        //1.调用service处理
        ordersService.sumbit(orders);

        //2.响应处理结果
        return R.success("下单成功！");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                                String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime) {
        Page<Orders> list = ordersService.ordersPage(page, pageSize, number, beginTime, endTime);

        return R.success(list);
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> orderPageDetail(Integer page, Integer pageSize){
        Page<OrdersDto> detailPage=ordersService.orderPageDetail(page,pageSize);
        return R.success(detailPage);
    }

    @PutMapping
    public R<String> update(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

}
